package printing;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PrintingDevice(defaultPrintMethod = "print", defaultNumberOfCopies = 5 )
public class Printer<T extends ICartridge> implements IMachine
{

	private Machine machine;
	public String modelNumber;
	private PaperTray paperTray = new PaperTray();
	private T cartridge;
//	private List<Page> pages = new ArrayList<Page>();
	private Map<Integer, Page> pagesMap = new HashMap<Integer, Page>();
	
	public Printer (boolean isOn, String modelNumber, T cartridge)
	{
		machine = new Machine (isOn);
		this.modelNumber = modelNumber;
		this.cartridge = cartridge;
	}
	
	public <U extends ICartridge> void printUsingCartridge(U cartridge, String message)
	{
//		System.out.println(cartridge.toString());
		System.out.println(message);
//		System.out.println(cartridge.toString());
	}
	
	public  T getCartridge ()
	{
		return  cartridge;
	}
	
	
	public void TurnOn()
	{
		System.out.println("Warming up the Printer!");
		machine.TurnOn();
	}
	
	public void print(int copies)
	{
		checkCopies(copies);
		
		String onStatus = "";
		if (machine.isOn())
		{
			onStatus = "is ON!";
		}
		else
			onStatus = "is OFF!";
		String textToPrint = getTextFromFile();
		
		
	/*	String textToPrint = modelNumber + onStatus;
		textToPrint += "|||"+cartridge.printColor()+"|||";*/
		int pageNumber = 1;

		while(copies>0 && paperTray.isEmpty())
		{
//			System.out.println(textToPrint);
//			pages.add(new Page(textToPrint));
			pagesMap.put(pageNumber, new Page(textToPrint + " : " + pageNumber));
//			System.out.println(pagesMap.get(pageNumber).getText());
			copies--;
			pageNumber++;
			paperTray.usePages();
		}
		if (!paperTray.isEmpty())
			System.out.println("Load more paper");
		
	}
	
    private String getTextFromFile()
    {   String allText = "";
    	FileReader reader = null;
    	CapitalizedReader capReader = null;
    	try {
			reader = new FileReader("D:\\javaIOFile.txt");
			BufferedReader bReader = new BufferedReader(reader);
			capReader = new CapitalizedReader (bReader);
			
			String line;
			while ( (line = capReader.readLine()) != null)
			{
				allText += line +"\n";
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally 
    	{
    		if (capReader !=null)
				try {
					capReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
    	}
    	return allText;
    }

	
	
	


//	public void outputPages()
//	{
//		for (Page currentPage : pages)
//			System.out.println(currentPage.getText());
//	}
    public void outputPage (int pageNumber)
	{
    	PrintWriter writer = null;
    	try
    	{
    		writer = new PrintWriter(new FileWriter("D:\\outputpage.txt"));
    		writer.println(pagesMap.get(pageNumber).getText());
    	} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally
    	{
    		if (writer != null)
    			writer.close();
    	}
	}
    
    
//	public void outputPage (int pageNumber)
//	{
//		System.out.println(pagesMap.get(pageNumber).getText());
//	}

	private void checkCopies(int copies) {
		if (copies <0)
			throw new IllegalArgumentException("Can't print less than 0 copies");
	}
   
	public void printColors ()
	{
		String[] colors = new String[] {"Red", "Blue", "Green", "Yellow"};
		for (String currentColor : colors)
		{
			if("Green".equals(currentColor))
			{
				continue;
			}
			System.out.println(currentColor);
		}
		
		
	}
	public void print(String text)
	{
		System.out.println(text);
	}
	public String getModelNumber()
	{
		return modelNumber;
	}

	public void loadPaper(int count) {
		paperTray.addPaper(count);
		 
		// TODO Auto-generated method stub
		
	}
	

	@Override
	public void TurnOff() {
		// TODO Auto-generated method stub
		machine.TurnOff();
	}
	@Override
	public boolean isOn() {
		// TODO Auto-generated method stub
		return machine.isOn;
	}

}
